*Define programs---------------------------------------------------------------
program drop _all
*Rank vars relative within shina
program define relrank
	*#1: variable to rank
	*#2: output variable
	sort shina_id `1'
	gen `2'=1 if shina_id!=shina_id[_n-1]
	local n=1
	while `n'<_N{
		local n0=`n'-1
		if shina_id[`n']==shina_id[`n0']{
			if `1'[`n']==`1'[`n0']{
				replace `2'=`2'[`n0'] if _n==`n'
				}
			if `1'[`n']!=`1'[`n0']{
				replace `2'=`2'[`n0']+1 if _n==`n'
				}
		}
		local n=`n'+1
		}
	replace `2'=. if missing(`1')
end
*Rank vars relative within shina-leader
program define relrank_ldr
	*#1: variable to rank
	*#2: output variable
	sort shina_id leader_id `1'
	gen `2'=1 if shina_id!=shina_id[_n-1] | leader_id!=leader_id[_n-1]
	local n=1
	while `n'<_N{
		local n0=`n'-1
		if shina_id[`n']==shina_id[`n0'] & leader_id[`n']==leader_id[`n0']{
			if `1'[`n']==`1'[`n0']{
				replace `2'=`2'[`n0'] if _n==`n'
				}
			if `1'[`n']!=`1'[`n0']{
				replace `2'=`2'[`n0']+1 if _n==`n'
				}
		}
		local n=`n'+1
		}
	replace `2'=. if missing(`1')
end

*Parameters--------------------------------------------------------------------
local exch=2288 //Exchange rate TSH per USD

*=================================================================*
*--------------------Clean up data for regressions----------------*
*=================================================================*
use "${sdir}/Leaders_and_Owners_cln.dta", clear


*Create ranks------------------------------------------------------------------

*within-shina ranking outcomes
relrank s9_q_1_own rank_wtp 		//willingness-to-pay

*flip leader rankings and create within-shina ranks (so that unobserved respondents not included in ranks)
gen tmp_wtp=16-q41_ldr 	if owner_observed==1

relrank_ldr tmp_wtp 	rank_wtp_ldr
*drop tmp_*


*Top and bottom ranks
bysort shina_id: egen maxrelrank=max(rank_wtp)
gen toprank_wtp=(maxrelrank==rank_wtp) if owner_observed==1
gen botrank_wtp=(rank_wtp==1) if owner_observed==1
bysort shina_leader_id: egen maxrelrank_ldr=max(rank_wtp_ldr)
gen toprank_wtp_ldr=(maxrelrank_ldr==rank_wtp_ldr) if owner_observed==1
gen botrank_wtp_ldr=(rank_wtp_ldr==1) if owner_observed==1

*top and bottom of full distribution
bysort shina_plot_id: gen check=_n
sum s9_q_1_own if check==1, d
gen p90wtp=(s9_q_1_own>`r(p90)') if missing(s9_q_1_own)==0
gen p10wtp=(s9_q_1_own<`r(p10)') if missing(s9_q_1_own)==0
gen p90wtp_ldr=(q42_ldr>`r(p90)') if missing(s9_q_1_own)==0
gen p10wtp_ldr=(q42_ldr<`r(p10)') if missing(s9_q_1_own)==0
drop check


*Physical characteristics of property------------------------------------------
*valuation
gen value=(value_pltpht_own/`exch')/1000
relrank value rank_value
xtile pctl_value		=value, nq(100)
bysort shina_id: egen maxrelvalue=max(rank_value)
gen toprank_value=(maxrelvalue==rank_value) if owner_observed==1
gen botrank_value=(rank_value==1) if owner_observed==1
bysort shina_plot_id: gen check=_n
sum value if check==1, d
gen p90value=(value<`r(p10)') if missing(value)==0
gen p10value=(value<`r(p10)') if missing(value)==0
drop check
*invoice
gen invoice=invoice_value_own/`exch'
relrank invoice rank_invoice
xtile pctl_invoice		=invoice, nq(100)
bysort shina_id: egen maxrelinvoice=max(rank_invoice)
gen toprank_invoice=(maxrelinvoice==rank_invoice) if owner_observed==1
gen botrank_invoice=(rank_invoice==1) if owner_observed==1
bysort shina_plot_id: gen check=_n
sum invoice if check==1, d
gen p90invoice=(invoice<`r(p10)') if missing(invoice)==0
gen p10invoice=(invoice<`r(p10)') if missing(invoice)==0
drop check

*area
gen area=lot_area_inv_obl
relrank area rank_area
bysort shina_id: egen maxrelarea=max(rank_area)
gen toprank_area=(maxrelarea==rank_area) if owner_observed==1
gen botrank_area=(rank_area==1) if owner_observed==1
bysort shina_plot_id: gen check=_n
sum area if check==1, d
gen p90area=(area<`r(p10)') if missing(area)==0
gen p10area=(area<`r(p10)') if missing(area)==0
drop check

tab empty_lot_pltpht_own
tab gated_pltpht_own
tab walls_pltpht_own 
tab roof_pltpht_own 

gen x_built_lot=1-empty_lot_pltpht_own
gen x_gated=cond(missing(gated_pltpht_own)==0,gated_pltpht_own,0)
gen x_goodroof=(roof_pltpht_own==2)
gen x_goodwalls=(walls_pltpht_own==3)
label var x_built_lot "Built Plot"
label var x_gated "Gated Plot"
label var x_goodroof "Good Roof"
label var x_goodwalls "Good Walls"
label var area "Plot Size"

*make within plot id
bysort shina_id plot_id: gen within_plot_id=_n

*Rename and relabel------------------------------------------------------------
rename (s9_q_1_own q42_ldr)(wtp wtp_ldr)
replace wtp=wtp/`exch'
replace wtp_ldr=wtp_ldr/`exch'

label var rank_wtp 		"\makecell{WTP\\Rank}"
label var wtp 			"WTP"
label var toprank_wtp 	"\makecell{Top\\Rank}"
label var botrank_wtp 	"\makecell{Bottom\\Rank}"
label var p90wtp 		"\makecell{Top\\10\%}"
label var p10wtp 		"\makecell{Bottom\\10\%}"

save "temp/run_predictions_wtp.dta", replace

*=================================================================*
*quick correlation table
use "temp/run_predictions_wtp.dta", clear
bysort shina_plot_id: gen check=_n
corr wtp_ldr value
corr wtp_ldr area
corr wtp_ldr invoice
corr value area invoice if check==1
eststo: reg value x_* area if check==1

esttab using "tables/hedonic_value_referee.tex", se label nostar r2 replace
estimates clear

*=================================================================*
*------------Physical Characteristics Prediction Regressions------* 
*=================================================================*
use "temp/run_predictions_wtp.dta", clear

local p_lst "" "rank_" "toprank_" "botrank_" "p90" "p10"
local c=0
foreach pfx in "`p_lst'" {
	preserve
	di "`pfx'wtp"
	local v "`pfx'wtp"
	gen temp=`v'_ldr
	gen temp_inv=`pfx'invoice
	gen temp_val=`pfx'value
	gen temp_area=`pfx'area
	
	foreach spc in "" "temp"{
		local ++c
		
		*Panel A: Invoice
		eststo pnl1_`c': reghdfe `v' temp_inv `spc', vce(cluster shina_leader_id) absorb(strata trt_grp_ldr)
		local wR2di: di %5.4f e(r2_a_within)
		estadd local wR2="`wR2di'"
		
		*Panel B: Property Value
		eststo pnl2_`c': reghdfe `v' temp_val `spc', vce(cluster shina_leader_id) absorb(strata trt_grp_ldr)
		local wR2di: di %5.4f e(r2_a_within)
		estadd local wR2="`wR2di'"
		
		*Panel C: Observable Characteristics
		eststo pnl3_`c': reghdfe `v' temp_area x_* `spc', vce(cluster shina_leader_id) absorb(strata trt_grp_ldr)
		local wR2di: di %5.4f e(r2_a_within)
		estadd local wR2="`wR2di'"
	}

	restore
	}
	
gen temp=.
gen temp_inv=.
gen temp_val=.
gen temp_area=.
label var temp "Prediction"
label var temp_inv "Invoice"
label var temp_val "Property Value"
label var temp_area "Plot Size"
	
*write to file
local note ///
	This table presents coefficients from regressions of willingness-to-pay on leader's predictions of willingness-to-pay and observable characteristics of the property. Each observation is a leader-plot owner pair. Each column is a different transformation of willingness-to-pay: column 1 in USD, column 2 is the within neighbourhood rank, column 3 is an indicator for the highest in the neighbourhood, and column 4 is an indicator for the lowest. Leader treatment group and randomization strata fixed effects are always included as controls. Each panel represents a different set of observable characteristics: panel A uses the invoice value, panel B estimated property value, and panel C uses property features (plot size, an indicator if the plot is built upon, if it is gated, if it's roof is painted, and if its walls are plastered). Standard errors in parentheses are clustered at leader level. \sym{*}\(p<0.10\),	\sym{**}\(p<0.05\), \sym{***}\(p<0.01\).
	
		
*put all panels together in one table
//top panel 
esttab pnl1* using "tables/predictions_wtp_observables.tex", ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N wR2, label("N" "within-R$^2$")) ///
	se label wrap fragment ///
	nobaselevels nocons ///
	prehead("\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} {\centering\begin{tabular}{l*{12}{c}} \hline\hline") ///
	posthead("\hline \\ \multicolumn{5}{l}{\textit{Panel A: Invoice Value}} \\\\[-1ex]") ///
	replace 

//middle panel #1
esttab pnl2* using "tables/predictions_wtp_observables.tex", ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N wR2, label("N" "within-R$^2$")) ///
	se label wrap fragment ///
	nomtitles nonumbers ///
	nobaselevels nocons ///
	posthead("\hline \\ \multicolumn{5}{l}{\textit{Panel B: Property Value (1,000 USD)}} \\\\[-1ex]") ///
	append

//bottom panel 
esttab pnl3* using "tables/predictions_wtp_observables.tex", ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N wR2, label("N" "within-R$^2$")) ///
	se label wrap fragment ///
	nomtitles nonumbers ///
	nobaselevels nocons ///
	posthead("\hline \\ \multicolumn{5}{l}{\textit{Panel C: Property Features}} \\\\[-1ex]") ///
	prefoot("\hline") ///
	postfoot("\hline\hline \end{tabular}\par\medskip}{\footnotesize {\it Note: }`note'}") ///
	append
estimates clear






*END--------
clear
